package com.ruitu.mall_98_seller.ui.home;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.text.TextUtils;
import android.view.MenuItem;

import com.ruitu.arad.Arad;
import com.ruitu.arad.api.BaseResult;
import com.ruitu.arad.base.BaseActivity;
import com.ruitu.arad.support.view.NoScrollViewPager;
import com.ruitu.arad.util.MD5Util;
import com.ruitu.arad.util.SizeUtils;
import com.ruitu.arad.util.ToastUtils;
import com.ruitu.mall_98_seller.R;
import com.ruitu.mall_98_seller.mvp.model.UserModel;
import com.ruitu.mall_98_seller.support.widget.BottomNavigationViewEx;
import com.ruitu.router_module.AppHolder;
import com.ruitu.router_module.Logg;
import com.ruitu.router_module.bean.EventModel;
import com.ruitu.router_module.bean.User;

import cn.jpush.android.api.JPushInterface;
import io.reactivex.functions.Consumer;
import q.rorbin.badgeview.QBadgeView;

import static com.ruitu.mall_98_seller.ui.user.LoginActivity.PASSWORD_KEY;
import static com.ruitu.mall_98_seller.ui.user.LoginActivity.USER_NAME_KEY;

public class HomeActivity extends BaseActivity {
    private BottomNavigationViewEx bnve;
    private NoScrollViewPager vp;
    private QBadgeView qBadgeView;//小红点

    private VpAdapter adapter;

    @Override
    protected int getLayoutRes() {
        return R.layout.activity_home;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        bnve = findViewById(R.id.bnve);
        vp = findViewById(R.id.vp);

        qBadgeView = new QBadgeView(this);

        vp.setOffscreenPageLimit(9);
        vp.setScanScroll(false);

        bnve.enableAnimation(false);
        bnve.enableShiftingMode(false);
        bnve.enableItemShiftingMode(false);

        bnve.setIconsMarginTop(SizeUtils.dp2px(10));
        bnve.setIconSize(23, 23);
        bnve.setTextSize(10);
        bnve.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                return true;
            }
        });

        adapter = new VpAdapter(getSupportFragmentManager());
        vp.setAdapter(adapter);
        bnve.setupWithViewPager(vp);

        // 自动登录
        String uName = Arad.preferences.getString(USER_NAME_KEY);
        String pwd = Arad.preferences.getString(PASSWORD_KEY);
        if (!TextUtils.isEmpty(uName) && !TextUtils.isEmpty(pwd)) {
            String md5Pwd = MD5Util.md5String(pwd);
            rxList.add(new UserModel.LoginM().login(uName, md5Pwd).subscribe(new Consumer<BaseResult<User>>() {
                @Override
                public void accept(BaseResult<User> result) throws Exception {
                    if (result.isSuccess()) {
                        AppHolder.instance().setUser(result.getData());
                        String uId = result.getData().getId();
                        String uId9 = uId.substring(0, 9);
                        int sequence = Integer.parseInt(uId9);
                        AppHolder.instance().setSequence(sequence);
                        JPushInterface.setAlias(Arad.app, AppHolder.instance().getSequence(), uId);// 极光别名
                        Arad.bus.post(new EventModel(19090653));// 首页自动登录成功
                        Arad.bus.post(new EventModel(19090553));// 通知连接融云
                        Arad.bus.post(new EventModel(19090651));// 获取各种数量
                    }
                }
            }));
        } else {
            JPushInterface.setAlias(Arad.app, AppHolder.instance().getSequence(), "1111111111");// 极光别名
        }
    }

    // 设置底部导航栏消息数量
    public void addBadge(int position, final int number) {// add badge
        int num = number > 99 ? 99 : number;
        if (number == 0) {
            qBadgeView.hide(true);
        } else {
            Logg.i("消息总数量 number = " + number);
            qBadgeView.setBadgeNumber(num)
                    .bindTarget(bnve.getBottomNavigationItemView(position))
                    .setGravityOffset(40, 3, true)
                    .setBadgeTextSize(10, true);
        }
    }

    // 设置跳转首页的fragment  注意下标不要越界
    public void setNavigationPage(int index) {
        vp.setCurrentItem(index, true);
    }

    private class VpAdapter extends FragmentPagerAdapter {

        public VpAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            if (position == 0) {// 首页,工作台
                return WorkFragment.newInstance();
            } else if (position == 1) {// 消息
                return MsgFragment.newInstance();
            } else if (position == 2) {// 我的
                return MineFragment.newInstance();
            }

            return WorkFragment.newInstance();
        }

        @Override
        public int getCount() {
            return 3;
        }
    }

    private long waitTime = 2000;
    private long touchTime = 0;

    @Override
    public void onBackPressed() {
        if (null != vp && vp.getCurrentItem() == 0) {
            long currentTime = System.currentTimeMillis();
            if ((currentTime - touchTime) >= waitTime) {
                ToastUtils.showShortSafe("再按一次退出程序");
                touchTime = currentTime;
            } else {
                Intent backHome = new Intent(Intent.ACTION_MAIN);//启动一个意图,回到桌面
                backHome.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                backHome.addCategory(Intent.CATEGORY_HOME);
                startActivity(backHome);
//                System.exit(0);
//                super.onBackPressed();
            }
        } else {
            vp.setCurrentItem(0);
        }
    }
}
